<script>
export default {
  name: 'App',
  data() {
    return {
      // 表格数据
      tableData: [
        { id: 1, name: '项目1', description: '这是项目1的描述' },
        { id: 2, name: '项目2', description: '这是项目2的描述' }
      ],
      // 搜索关键词
      searchQuery: '',
      // 选中的行
      selectedRows: [],
      // 对话框显示状态
      dialogVisible: false,
      // 是否是编辑模式
      isEdit: false,
      // 当前编辑的行
      currentRow: null,
      // 表单数据
      form: {
        name: '',
        description: ''
      },
      // 表单验证规则
      rules: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    // 过滤后的表格数据
    filteredTableData() {
      if (!this.searchQuery) return this.tableData
      const query = this.searchQuery.toLowerCase()
      return this.tableData.filter(item => 
        item.name.toLowerCase().includes(query) || 
        item.description.toLowerCase().includes(query)
      )
    },
    // 对话框标题
    dialogTitle() {
      return this.isEdit ? '编辑' : '添加'
    }
  },
  methods: {
    // 处理搜索
    handleSearch() {
      // 搜索逻辑已通过计算属性实现
    },
    // 处理添加
    handleAdd() {
      this.isEdit = false
      this.form = { //清空表单
        name: '',
        description: ''
      }
      this.dialogVisible = true
    },
    // 处理编辑
    handleEdit(row) {
      this.isEdit = true
      this.currentRow = row
      this.form = { ...row }//将数据传递给表单
      this.dialogVisible = true
    },
    // 处理删除
    handleDelete(row) {
      this.$confirm('确认删除该条数据吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const index = this.tableData.findIndex(item => item.id === row.id)
        if (index !== -1) {
          this.tableData.splice(index, 1)
          this.$message.success('删除成功')
        }
      }).catch(() => {})
    },
    // 处理批量删除
    handleBatchDelete() {
      if (!this.selectedRows.length) return
      this.$confirm(`确认删除选中的 ${this.selectedRows.length} 条数据吗？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = this.selectedRows.map(row => row.id)
        this.tableData = this.tableData.filter(item => !ids.includes(item.id))
        this.selectedRows = []
        this.$message.success('批量删除成功')
      }).catch(() => {})
    },
    // 处理表格选择变化
    handleSelectionChange(selection) {
      this.selectedRows = selection
    },
    // 提交表单
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            // 编辑模式
            const index = this.tableData.findIndex(item => item.id === this.currentRow.id)
            if (index !== -1) {
              this.tableData[index] = { ...this.currentRow, ...this.form } //浅拷贝保留原有属性，更新表单中修改的字段
              this.$message.success('修改成功')
            }
          } else {
            // 添加模式
            const newId = Math.max(...this.tableData.map(item => item.id), 0) + 1
            this.tableData.push({
              id: newId,
              ...this.form
            })
            this.$message.success('添加成功')
          }
          this.dialogVisible = false
        }
      })
    }
  }
}
</script>

<template>
  <div id="app">
    <el-container>
      <el-main>
        <el-card>
          <div slot="header" class="clearfix">
            <span>表格管理</span>
          </div>
          
          <!-- 搜索和添加区域 -->
          <div class="operation-area">
            <el-input
              v-model="searchQuery"
              placeholder="请输入搜索内容"
              style="width: 200px; margin-right: 10px"
              @input="handleSearch"
            ></el-input>
            <el-button type="primary" @click="handleAdd">添加</el-button>
            <el-button type="danger" @click="handleBatchDelete" :disabled="!selectedRows.length">批量删除</el-button>
          </div>

          <!-- 表格区域 -->
          <el-table
            :data="filteredTableData"
            style="width: 100%; margin-top: 20px"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column prop="name" label="名称"></el-table-column>
            <el-table-column prop="description" label="描述"></el-table-column>
            <el-table-column label="操作" width="200">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="primary"
                  @click="handleEdit(scope.row)"
                >编辑</el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>

        <!-- 添加/编辑对话框 -->
        <el-dialog
          :title="dialogTitle"
          :visible.sync="dialogVisible"
          width="30%"
        >
          <el-form :model="form" :rules="rules" ref="form" label-width="80px">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" :disabled="isEdit"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="description">
              <el-input v-model="form.description"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
          </span>
        </el-dialog>
      </el-main>
    </el-container>
  </div>
</template>

<style>
.operation-area {
  margin-bottom: 20px;
}
</style>
